package com.tj.view.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.tj.view.tool.DisplayUtil;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class FlyView {

    // 旋转角度
    public static final int ROTATION = 1080;
    // 缩放的偏移
    public static final float SCALE_OFFSET = 0.5f;
    // 提前结束的时间
    public static final int AHEAD_TIME = 150;
    // 结束动画的监听
    private OnEndAnim mOnEndAnim;
    private Context mContext;
    private WindowManager mWm;
    // 初始位置X
    private int mOriX;
    // 初始位置Y
    private int mOriY;
    // 目的X
    private int mDesX;
    // 目的Y
    private int mDesY;
    // 缩小的比例
    private float mScale;
    // 图标大小（用于计算由于缩放带来的偏移）
    private float mIconHeight;
    private float mIconWidth;
    // 视图内容对象
    private ImageView mContentImageView;
    // 图片资源
    private Drawable mImageDrawable;
    // 容器布局
    private FrameLayout mContainerFrameLayout;

    public FlyView(Context context) {
        this.mContext = context;
        mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    }

    /**
     * 初始化参数 <功能简述>
     *
     * @param res
     * @param oriX
     * @param oriY
     * @param desX
     * @param desY
     */
    public void init(Drawable res, int oriX, int oriY, int desX, int desY,
                     float scale, int iconHeight, int iconWidth) {
        int statusHeight = DisplayUtil.getStatusHeight((Activity) mContext);
        this.mImageDrawable = res;
        this.mOriX = oriX;
        this.mOriY = oriY - statusHeight;//减去状态的高度？
        this.mDesX = desX;
        this.mDesY = desY - statusHeight;
        this.mScale = scale;
        this.mIconHeight = iconHeight;
        this.mIconWidth = iconWidth;
    }

    /**
     * 根据初始imageview和最终的view确定位置 <功能简述>
     *
     * @param imageView
     * @param desView
     */
    public void initByView(ImageView imageView, View desView) {
        int[] oriLocation = new int[2];
        imageView.getLocationInWindow(oriLocation);
        int[] desLocation = new int[2];
        desView.getLocationInWindow(desLocation);

        init(imageView.getDrawable(), oriLocation[0], oriLocation[1],
                desLocation[0], desLocation[1],
                computeScale(imageView, desView),
                imageView.getHeight(),
                imageView.getWidth());
    }

    /**
     * 开始飞入动画 <功能简述>
     */
    public void startFly() {
        // 设置容器
        setContainer();
        // 首先添加图片
        addContent();
        // 执行动画
        performAnim();
    }

    /**
     * 全部结束后释放资源，解绑wm <功能简述>
     */
    public void release() {
        if (mContentImageView != null) {
            mContentImageView = null;
        }
        if (mContainerFrameLayout != null
                && mContainerFrameLayout.getParent() != null) {
            mWm.removeView(mContainerFrameLayout);
        }
    }

    public void setOnEndAnim(OnEndAnim mOnEndAnim) {
        this.mOnEndAnim = mOnEndAnim;
    }

    /**
     * 计算缩放比例 <功能简述>
     *
     * @param oriView
     * @param desView
     * @return
     */
    private float computeScale(View oriView, View desView) {
        float scaleH = ((float) desView.getHeight())
                / ((float) oriView.getHeight());
        float scaleW = ((float) desView.getWidth())
                / ((float) oriView.getWidth());
        return scaleH < scaleW ? scaleH : scaleW;
    }

    // 添加内容到容器中
    private void addContent() {
        mContentImageView = new ImageView(mContext);
        mContentImageView.setImageDrawable(mImageDrawable);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
                android.widget.FrameLayout.LayoutParams.WRAP_CONTENT);

        // 固定位置
        lp.setMargins(mOriX, mOriY, 0, 0);
        // 如果view没有被加入到某个父组件中，则加入WindowManager中
        if (mContentImageView.getParent() == null) {
            mContainerFrameLayout.addView(mContentImageView, lp);
        }
    }

    // 设置容器，装载动画的view
    private void setContainer() {
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        // 不允许触摸获取焦点
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        // 设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        mContainerFrameLayout = new FrameLayout(mContext);
        // 宽高适应内容
        wmParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
        wmParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
        // 如果view没有被加入到某个父组件中，则加入WindowManager中
        if (mContainerFrameLayout.getParent() == null) {
            mWm.addView(mContainerFrameLayout, wmParams);
        }
    }

    // 执行动画
    private void performAnim() {
        // 移动，旋转，缩放，透明度
        AnimatorSet allAnimSet = new AnimatorSet();
        // 1.旋转
        ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "rotation", ROTATION);
        // 2.移动X
        ObjectAnimator transXAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "translationX", mDesX - mOriX
                        - ((1 - mScale) * mIconWidth) / 2);
        // 3.移动Y
        ObjectAnimator transYAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "translationY", mDesY - mOriY
                        - ((1 - mScale) * mIconHeight) / 2);
        // 4.缩放X
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "scaleX", mScale * SCALE_OFFSET);
        // 5.缩放Y
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "scaleY", mScale * SCALE_OFFSET);

        // 6.透明度
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(
                mContentImageView, "alpha", 0.2f);

        allAnimSet.playTogether(rotationAnimator, transXAnimator,
                transYAnimator, scaleXAnimator, scaleYAnimator, alphaAnimator);
        allAnimSet.setDuration(1000).start();
    }

    public interface OnEndAnim {
        void onEnd();
    }
}
